package com.qt.socket.work;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

/**
 * @Author zsf
 * @Date 2023/10/5 1:17
 * @Version 1.0
 */
public class SendMessageWork {

    /**
     * 按照下面的要求实现程序
     * UDP发送数据:数据来自于键盘录入，直到输入的数据是886，发送数据结束UDP接收数据:因为接收端不知道发送端什么时候停止发送，故采用死循环接收
     *
     * @param args
     * @throws IOException
     */

    public static void main(String[] args) throws IOException {

        DatagramSocket ds = new DatagramSocket();
        Scanner sc = new Scanner(System.in);
        InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
        while (true) {
            String msg = sc.nextLine();
            byte[] data = msg.getBytes();
            DatagramPacket dp = new DatagramPacket(data,data.length,inetAddress,8888);
            ds.send(dp);
            if("886".equals(msg)){
                break;
            }
        }

        ds.close();


    }


}
